package com.example.othree.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.Drone2;
import com.example.othree.entity.FlightDataCount;
import com.example.othree.service.Drone2Service;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/api")
public class Drone2Controller {
    @Autowired
    private Drone2Service drone2Service;


    @PostMapping("/drone2/listPage")
    @ApiOperation("查询分页")
    public R listPage(@RequestBody PageParam pageParam) {
        // 1. 检查 pageParam 是否为 null
        if (pageParam == null) {
            pageParam = new PageParam();
        }

        // 2. 获取 param 并检查是否为 null
        HashMap<String, Object> param = pageParam.getParam();
        if (param == null) {
            param = new HashMap<>();
        }

        // 3. 安全地获取参数值
        String equipmentName = String.valueOf(param.getOrDefault("equipmentName", ""));
        String detectionDate = String.valueOf(param.getOrDefault("detectionDate", ""));
        String times = String.valueOf(param.getOrDefault("times", ""));

        // 4. 创建分页对象
        Page<Drone2> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        // 5. 创建查询条件
        LambdaQueryWrapper<Drone2> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(equipmentName) && !"null".equals(equipmentName)) {
            lambdaQueryWrapper.eq(Drone2::getEquipmentName, equipmentName);
        }

        if (StringUtils.isNotBlank(detectionDate) && !"null".equals(detectionDate)) {
            lambdaQueryWrapper.eq(Drone2::getDetectionDate, detectionDate);
        }

        if (StringUtils.isNotBlank(times) && !"null".equals(times)) {
            lambdaQueryWrapper.eq(Drone2::getTimes, times);
        }

        // 6. 执行查询
        IPage<Drone2> result = drone2Service.PageList(page, lambdaQueryWrapper);

        // 7. 返回结果
        return R.ok().data("result", result);
    }

    @PostMapping("/drone2/saveBatch")
    @ApiOperation("批量添加")
    public R saveBatch(@RequestBody List<Drone2> droneList){
        return drone2Service.saveBatch(droneList) ? R.ok() : R.error();
    }

    @PostMapping("/drone2/update")
    @ApiOperation("修改")
    public R update(@RequestBody Drone2 drone2){
        return drone2Service.updateById(drone2) ? R.ok() : R.error();
    }

    @PostMapping("/drone2/delete/{id}")
    public R delete(@PathVariable Integer id){
        System.out.println("success？？？");
        return drone2Service.removeById(id) ? R.ok() : R.error();
    }

    //根据气象站名获取飞行日期和对应次数
    @GetMapping("/drone2/flight-count")
    public R getFlightData(@RequestParam("equipmentName") String equipmentName) {
        List<FlightDataCount> flightDataCounts = drone2Service.getFlightDataCountByEquipmentName(equipmentName);
        return R.ok().data("data",flightDataCounts);
    }

    //根据气象站名和监测日期、飞行次序查询相关数据
    @GetMapping("/drone2/getDrone2ByCondition")
    public R getEquipmentData(
            @RequestParam String equipmentName,
            @RequestParam String detectionDate,
            @RequestParam Integer times) {

        List<Drone2> res = drone2Service.getEquipmentByCondition(equipmentName, detectionDate, times);
        return R.ok().data("data",res);
    }
}
